package org.hobbyhorse.ranks.client.desktop;

import org.hobbyhorse.ranks.client.ClientFactory;
import org.hobbyhorse.ranks.client.RanksAppShell;
import org.hobbyhorse.ranks.client.place.WelcomePlace;
import org.hobbyhorse.ranks.client.ui.LeftPanel;
import org.hobbyhorse.ranks.client.ui.OneWidgetLayoutPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckLayoutPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HasOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import java.util.ArrayList;
import java.util.List;

public class RanksAppShellDesktop extends ResizeComposite implements RanksAppShell {

	
	interface RanksAppShellDesktopUiBinder extends UiBinder<Widget, RanksAppShellDesktop> {	}

	private static RanksAppShellDesktopUiBinder uiBinder = GWT.create(RanksAppShellDesktopUiBinder.class);

	  /**
	   * CSS override used for the main menu.
	   */
	  interface MainMenuStyle extends CellList.Style { }
	
	  /**
	   * A ClientBundle that provides images for this widget.
	   */
	  interface Resources extends CellList.Resources {
	    /**
	     * The styles used in the main menu. We extend
	     * {@link CellList.Style#DEFAULT_CSS} with the styles defined in
	     * MainMenuCellList.css.
	     */
	    @Source({"MainMenuCellList.css", CellList.Style.DEFAULT_CSS})
	    MainMenuStyle cellListStyle();
	  }

//	@UiField HeaderDesktop header;
 //   @UiField(provided=true) 
  //  LeftPanel leftPanel;
//    @UiField OneWidgetLayoutPanel rightPanel;

	  @UiField
	  Anchor helpLink;

	  /**
	   * The main menu list.
	   */
	  @UiField(provided = true)
	  CellList<MainMenuItem> mainMenu;

	  /**
	   * The container that holds content.
	   */
	  @UiField
	  DeckLayoutPanel contentContainer;

	  @UiField
	  DockLayoutPanel leftNav;

	  /**
	   * The container that holds the pie chart.
	   */
	  @UiField
	  HasOneWidget pieChartContainer;
	

	public RanksAppShellDesktop(ClientFactory clientFactory, final PlaceController placeController) {
//		leftPanel = new LeftPanel(clientFactory);
		
	    // Initialize the main menu.
	    Resources resources = GWT.create(Resources.class);
	    mainMenu = new CellList<MainMenuItem>(new MainMenuItem.Cell(), resources);
	    mainMenu.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

	    // We don't expect to have more than 30 menu items.
	    mainMenu.setVisibleRange(0, 30);

	    // Add items to the main menu.
	    final List<MainMenuItem> menuItems = new ArrayList<MainMenuItem>();
	    menuItems.add(new MainMenuItem("Cavalieri", new WelcomePlace("")) {
	      @Override
	      public boolean mapsToPlace(Place p) {
	        // Map to all TaskListPlace instances.
	        return p instanceof WelcomePlace;
	      }
	    });

	    menuItems.add(new MainMenuItem("Aggiungi cavaliere", new WelcomePlace("")));
	    mainMenu.setRowData(menuItems);

	    // Choose a place when a menu item is selected.
	    final SingleSelectionModel<MainMenuItem> selectionModel =
	        new SingleSelectionModel<MainMenuItem>();
	    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
	      public void onSelectionChange(SelectionChangeEvent event) {
	        MainMenuItem selected = selectionModel.getSelectedObject();
	        if (selected != null && !selected.mapsToPlace(placeController.getWhere())) {
	          placeController.goTo(selected.getPlace());
	        }
	      }
	    });
	    mainMenu.setSelectionModel(selectionModel);

		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setWidget(IsWidget w) {
		// TODO Auto-generated method stub
		
	}


}
